#include "editpost.h"
#include "ui_editpost.h"

EditPost::EditPost(int id,QList<DictData> sys_normal_disable,QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::EditPost)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/image/res/stm.jpg"));
    setWindowTitle("编辑岗位");
    setFixedSize(458,297);
    this->sys_normal_disable = sys_normal_disable;
    this->postId = id;
    post.postId = 0;
    if(id > 0){
        init();
    }

}

EditPost::~EditPost()
{
    delete ui;
}

void EditPost::on_btn_cancel_clicked()
{
    close();
}

void EditPost::init()
{
    QByteArray *data = api.getById(this->postId);
    QJsonObject obj = QJsonDocument::fromJson(*data).object();
    if(obj.value("code").toInt() != 200) return;
    post = Post::toPost(obj.value("data").toObject());
    ui->lineEdit_postName->setText(post.postName);
    ui->lineEdit_postCode->setText(post.postCode);
    ui->spinBox->setValue(post.postSort);
    post.status == "0" ? ui->radioButton_normal->setChecked(1) :ui->radioButton_stop->setChecked(1);
    ui->lineEdit_remark->setText(post.remark);
}


void EditPost::on_btn_ok_clicked()
{
    if(ui->lineEdit_postName->text().isEmpty()){
        ui->lineEdit_postName->setStyleSheet("border-radius:10px;border:1px solid red;");
        return;
    }else{
        ui->lineEdit_postName->setStyleSheet("border-radius:10px;border:1px solid gray;");
    }
    if(ui->lineEdit_postCode->text().isEmpty()){
        ui->lineEdit_postCode->setStyleSheet("border-radius:10px;border:1px solid red;");
        return;
    }else{
        ui->lineEdit_postCode->setStyleSheet("border-radius:10px;border:1px solid gray;");
    }
    post.postName = ui->lineEdit_postName->text();
    post.postCode = ui->lineEdit_postCode->text();
    post.postSort = ui->spinBox->value();
    post.status = ui->radioButton_normal->isChecked() ? "0" : "1";
    post.remark = ui->lineEdit_remark->text();
    QByteArray *data = api.editPost(postId,post);
    QJsonObject obj = QJsonDocument::fromJson(*data).object();
    if(obj.value("code").toInt() == 200){
        QMessageBox::information(this,"提示",obj.value("msg").toString());
        emit success();
        close();
    }
}

